package com.zsck.zsgy.app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.multidex.MultiDex
import cn.jpush.android.api.JPushInterface
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.facebook.stetho.Stetho
import com.jeremyliao.liveeventbus.LiveEventBus
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.zsck.commonbase.BaseApp
import com.zsck.zsgy.util.Constants
import com.zsck.zsgy.util.PrefserHelper
import update.UpdateAppUtils
import kotlin.properties.Delegates

class MyApplication : BaseApp() {


    companion object {
        private val TAG = "MyApplication"
        lateinit var instance: MyApplication

        var context: Context by Delegates.notNull()
            private set
    }

    override fun onCreate() {
        super.onCreate()
        context = applicationContext
        instance = this
        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)
        initSDKs()
        PrefserHelper.initPrefser(this)

        LiveEventBus
            .config()
            .lifecycleObserverAlwaysActive(true)
            .autoClear(false);


        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        SDKInitializer.initialize(this)
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL)

        //        if (LeakCanary.isInAnalyzerProcess(this)) {
//            //此过程专用于LeakCanary进行堆分析。在此过程中不应初始化应用程序。
//            return;
//        }
//        LeakCanary.install(this);
        val mWxApi: IWXAPI = WXAPIFactory.createWXAPI(this, Constants.WX_APPID, false)
        // 将该app注册到微信
        // 将该app注册到微信
        mWxApi.registerApp(Constants.WX_APPID)


        //极光推送
        JPushInterface.setDebugMode(true) //打印log

        JPushInterface.init(this)
    }

    private fun initSDKs() {
        MultiDex.install(this)
        Stetho.initializeWithDefaults(this)
        CrashReport.initCrashReport(applicationContext, Constants.BUGLY_APP_ID, false)
        UpdateAppUtils.init(this)
        /*消息事件总线*/
        LiveEventBus
            .config()
            .lifecycleObserverAlwaysActive(true)
            .autoClear(false)
    }

    private val mActivityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            Log.d(TAG, "onCreated: " + activity.componentName.className)
        }

        override fun onActivityStarted(activity: Activity) {
            Log.d(TAG, "onStart: " + activity.componentName.className)
        }

        override fun onActivityResumed(activity: Activity) {

        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {

        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

        }

        override fun onActivityDestroyed(activity: Activity) {
            Log.d(TAG, "onDestroy: " + activity.componentName.className)
        }
    }

}